#include "devupdatedlg.h"
#include "sysconf.h"
#include <QMessageBox>

extern Channel chn[2];
extern QMap<QString,LdInfo> updateLds;
extern QString ldeviceName(int chnIndex,int devIndex);

DevUpdateDLG::DevUpdateDLG(QWidget *parent) :
    QDialog(parent)
{
    setupUi(this);

    connect(updateBtn,SIGNAL(clicked()),this,SLOT(updateDev()));
    connect(closeBtn,SIGNAL(clicked()),this,SLOT(closeDlg()));
}

void DevUpdateDLG::setData(int chnIndex,int devIndex)
{
    chnInd=chnIndex;
    devInd=devIndex;
    chnEdit->setText(QString::number(chnIndex+1));
    typeEdit->setText(chn[chnIndex].sysdev.at(devIndex).dev_type);
    nameEdit->setText(chn[chnIndex].sysdev.at(devIndex).dev_name);
    addrEdit->setText(QString::number(chn[chnIndex].sysdev.at(devIndex).devAddr));
}


void DevUpdateDLG::updateDev()
{
    chn[chnInd].sysdev[devInd].dev_name=nameEdit->text();
    chn[chnInd].sysdev[devInd].devAddr=addrEdit->text().trimmed().toInt();
    QString ldname=ldeviceName(chnInd,devInd);
    LdInfo ld;
    ld.chnIndex=chnInd;
    ld.devIndex=devInd;
    ld.ldname=ldname;
    if(!updateLds.contains(ldname))
    {
        updateLds.insert(ldname,ld);
    }

       // qDebug() << i.next();

    QMessageBox::information(this,"提示","修改成功!");
    this->close();
}

void DevUpdateDLG::closeDlg()
{
    this->close();
}
